<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="教练姓名" prop="coachName">
        <el-input
          v-model="queryParams.coachName"
          placeholder="请输入教练姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练职称" prop="coachTitle">
        <el-select v-model="queryParams.coachTitle" placeholder="请选择教练职称" clearable>
          <el-option
            v-for="dict in dict.type.sys_coach_professional"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教练资质" prop="coachQualification">
        <el-select v-model="queryParams.coachQualification" placeholder="请选择教练资质" clearable>
          <el-option
            v-for="dict in dict.type.sys_coach_aptitude"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教练联系方式" prop="coachContactinfo">
        <el-input
          v-model="queryParams.coachContactinfo"
          placeholder="请输入教练联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="float: right;">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:coaches:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:coaches:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:coaches:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:coaches:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coachesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" type="index" />
      <el-table-column label="教练姓名" align="center" prop="coachName" width="120" />
      <el-table-column label="教练职称" align="center" prop="coachTitle" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_coach_professional" :value="scope.row.coachTitle"/>
        </template>
      </el-table-column>
      <el-table-column label="教练专业领域" align="center" prop="coachSpecialty" width="150" />
      <el-table-column label="教练资质" align="center" prop="coachQualification" width="250">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_coach_aptitude" :value="scope.row.coachQualification"/>
        </template>
      </el-table-column>
      <el-table-column label="教练头像" align="center" prop="coachAvatar" width="120">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coachAvatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="教练简介" align="center" prop="coachIntroduction" width="250"/>
      <el-table-column label="教练联系方式" align="center" prop="coachContactinfo"   width="200"/>
      
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleShowDetail(scope.row)"
            v-hasPermi="['system:coaches:show']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:coaches:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:coaches:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教练信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12" >
            <el-form-item label="教练姓名" prop="coachName">
              <el-input v-model="form.coachName" placeholder="请输入教练姓名" :disabled="ShowDetail"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="教练职称" prop="coachTitle">
              <el-select v-model="form.coachTitle" placeholder="请选择教练职称" style="width: 100%;" :disabled="ShowDetail">
                <el-option
                  v-for="dict in dict.type.sys_coach_professional"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" >
            <el-form-item label="教练专业领域" prop="coachSpecialty">
              <el-input v-model="form.coachSpecialty" placeholder="请输入教练专业领域" :disabled="ShowDetail"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="教练资质" prop="coachQualification">
              <el-select v-model="form.coachQualification" placeholder="请选择教练资质"  style="width: 100%;" :disabled="ShowDetail">
                <el-option
                  v-for="dict in dict.type.sys_coach_aptitude"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="教练简介" prop="coachIntroduction">
          <el-input v-model="form.coachIntroduction" type="textarea" placeholder="请输入内容" :disabled="ShowDetail"/>
        </el-form-item>
        <el-form-item label="教练头像" prop="coachAvatar">
          <image-upload v-model="form.coachAvatar" />
        </el-form-item>
        <!-- <el-form-item label="教练所教课程" prop="courseId">
          <el-input v-model="form.coachCourse" placeholder="杠铃塑形课"  :disabled="ShowDetail"/>
          <div v-for="(item,index) in coursesList  " :key='index' >
            <span>{{item.courseName}}</span>
            <span>{{ coursesList.courseName }}</span>
          </div>
        </el-form-item> -->
        <el-form-item label="教练联系方式" prop="coachContactinfo" style="width: 50%;">
          <el-input v-model="form.coachContactinfo" placeholder="请输入教练联系方式"  :disabled="ShowDetail"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCoaches, getCoaches, delCoaches, addCoaches, updateCoaches } from "@/api/system/coaches";
import { listCourses, getCourses } from "@/api/system/courses";

export default {
  name: "Coaches",
  dicts: ['sys_coach_aptitude', 'sys_coach_professional'],
  data() {
    return {
      coursesList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      ShowDetail:false,
      // 教练信息表格数据
      coachesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coachName: null,
        coachTitle: null,
        coachQualification: null,
        coachContactinfo: null,
        coachId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        coachName: [
          { required: true, message: "教练姓名不能为空", trigger: "blur" }
        ],
        coachTitle: [
          { required: true, message: "教练职称不能为空", trigger: "change" }
        ],
        coachSpecialty: [
          { required: true, message: "教练专业领域不能为空", trigger: "blur" }
        ],
        coachQualification: [
          { required: true, message: "教练资质不能为空", trigger: "change" }
        ],
        coachContactinfo: [
          { required: true, message: "教练联系方式不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    // this.getCoursesList(); 
  },
  methods: {
    // getCoursesList(){
    //   listCourses(this.queryParams).then(response => {
    //     this.coursesList = response.rows;
    //   });
    // },
    /** 查询教练信息列表 */
    getList() {
      this.loading = true;
      listCoaches(this.queryParams).then(response => {
        this.coachesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        coachId: null,
        coachName: null,
        coachTitle: null,
        coachSpecialty: null,
        coachQualification: null,
        coachIntroduction: null,
        coachAvatar: null,
        coachContactinfo: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.coachId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加教练信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const coachId = row.coachId || this.ids
      getCoaches(coachId).then(response => {
        this.form = response.data;
        this.open = true;
        this.ShowDetail = false;
        this.title = "修改教练信息";
      });
    },
    handleShowDetail(row){
      this.reset();
      const coachId = row.coachId || this.ids
      getCoaches(coachId).then(response => {
        this.form = response.data;
        this.open = true;
        getCourses(coachId).then(response =>{
          this.coursesList = response.data;
          console.log(this.coursesList)
        })
        this.ShowDetail = true;
        this.title = "教练推荐详情";
      });
      
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.coachId != null) {
            updateCoaches(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCoaches(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const coachIds = row.coachId || this.ids;
      this.$modal.confirm('是否确认删除教练信息编号为"' + coachIds + '"的数据项？').then(function() {
        return delCoaches(coachIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/coaches/export', {
        ...this.queryParams
      }, `coaches_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
